package postgres

import (
	"context"
	"github.com/jackc/pgx/v5"
	"testing"
)

func setupTestDB(t *testing.T) *pgx.Conn {
	conn, err := pgx.Connect(context.Background(), "postgres://postgres:еназеравесомепвд@localhost:5432/postgres")
	if err != nil {
		t.Fatalf("Unable to connect to database: %v\n", err)
	}

	_, err = conn.Exec(context.Background(), `
		DROP TABLE IF EXISTS comments;
		DROP TABLE IF EXISTS posts;
		CREATE TABLE posts (
    id SERIAL PRIMARY KEY,
    title TEXT NOT NULL,
    content TEXT NOT NULL,
    comments_enabled BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE comments (
    id SERIAL PRIMARY KEY,
    post_id INT NOT NULL,
    parent_id INT,
    content TEXT NOT NULL CHECK (LENGTH(content) <= 2000),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_post
        FOREIGN KEY(post_id)
            REFERENCES posts(id)
            ON DELETE CASCADE,
    CONSTRAINT fk_parent
        FOREIGN KEY(parent_id)
            REFERENCES comments(id)
            ON DELETE CASCADE
);`)
	if err != nil {
		t.Fatalf("Unable to setup test database: %v\n", err)
	}

	return conn
}

//TODO написать тесты
